﻿#region Libraries

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

#endregion

namespace XYZ_Inventory
{
    public partial class ProductRawMaterials : Page
    {
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProductRawMaterials();
                LoadRawMaterial();
            }
        }

        protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadProductRawMaterials();
        }

        private void LoadProductRawMaterials()
        {
            XYZDBContext dbContext = new XYZDBContext();
            int productId = Convert.ToInt32(ddlProduct.SelectedValue);
            GridView1.DataSource =
                dbContext.RawMaterialForProducts.Where(x => x.ProductID == productId)
                    .Join(dbContext.RawMaterialInventories, x => x.RawMaterialID,
                        y => y.RawMaterialID, (a, b) => new {a.RawMaterialID, b.RawMaterialName, a.Quantity}).ToList();
            GridView1.DataBind();
        }

        protected void ddlMaterialType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadRawMaterial();
        }

        private void LoadRawMaterial()
        {
            XYZDBContext dbContext = new XYZDBContext();
            List<RawMaterialInventory> i = dbContext.RawMaterialInventories.Where(
                x => x.RawMaterialType == ddlMaterialType.SelectedValue).ToList();
            ddlRawMaterial.DataTextField = "RawMaterialName";
            ddlRawMaterial.DataValueField = "RawMaterialID";
            ddlRawMaterial.DataSource = i;
            ddlRawMaterial.DataBind();
        }

        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            int productId = Convert.ToInt32(ddlProduct.SelectedValue);
            List<RawMaterialForProduct> products =
                dbContext.RawMaterialForProducts.Where(x => x.ProductID == productId)
                    .ToList();

            if (products.Any(p => Convert.ToInt32(ddlRawMaterial.SelectedValue).Equals(p.RawMaterialID)))
            {
                lblStatus.Text = "Raw material already in list";
                lblStatus.ForeColor = Color.Red;
                return;
            }

            RawMaterialForProduct product = new RawMaterialForProduct
            {
                ProductID = Convert.ToInt32(ddlProduct.SelectedValue),
                RawMaterialID = Convert.ToInt32(ddlRawMaterial.SelectedValue),
                Quantity = Convert.ToInt32(txtQty.Text)
            };
            dbContext.RawMaterialForProducts.AddObject(product);
            dbContext.SaveChanges();
            LoadProductRawMaterials();
            txtQty.Text = "";
            lblStatus.Text = "";
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            LoadProductRawMaterials();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            int productId = Convert.ToInt32(ddlProduct.SelectedValue);
            int materialId = Convert.ToInt32(e.Keys["RawMaterialID"]);
            RawMaterialForProduct product =
                dbContext.RawMaterialForProducts.FirstOrDefault(x => x.ProductID == productId &&
                                                                     x.RawMaterialID == materialId);

            dbContext.RawMaterialForProducts.DeleteObject(product);
            dbContext.SaveChanges();
            e.Cancel = true;
            LoadProductRawMaterials();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            GridView1.EditIndex = -1;
            LoadProductRawMaterials();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            int productId = Convert.ToInt32(ddlProduct.SelectedValue);
            int materialId = Convert.ToInt32(e.Keys["RawMaterialID"].ToString());
            RawMaterialForProduct product =
                dbContext.RawMaterialForProducts.FirstOrDefault(x => x.ProductID == productId &&
                                                                     x.RawMaterialID == materialId);
            GridViewRow row = GridView1.Rows[e.RowIndex];
            TextBox txtQuantity = (TextBox) row.FindControl("txtQuantity");
            if (product != null) product.Quantity = Convert.ToInt32(txtQuantity.Text);
            dbContext.SaveChanges();
            GridView1.EditIndex = -1;
            LoadProductRawMaterials();
        }
    }
}